package com.xqboss.web.controller.consumer;

import com.xqboss.apps.domain.consumer.ConsumerRecycle;
import com.xqboss.apps.domain.nft.NftNumber;
import com.xqboss.apps.dto.IdDto;
import com.xqboss.apps.dto.consumer.ConsumerRecycleQueryDto;
import com.xqboss.apps.dto.consumer.ConsumerRecycleSaveDto;
import com.xqboss.apps.service.consumer.ConsumerRecycleService;
import com.xqboss.apps.service.nft.NftNumberService;
import com.xqboss.apps.vo.consumer.ConsumerRecycleDetailVo;
import com.xqboss.apps.vo.consumer.ConsumerRecycleVo;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.PageList;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import com.xqboss.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户实名认证Controller
 *
 * @author xqboss
 * @date 2023-08-08
 */
@Api(tags = {"回收空投管理"})
@RestController
@RequestMapping("/web/consumer/recycle")
public class WebConsumerRecycleController extends BaseController {

    @Autowired
    private ConsumerRecycleService consumerRecycleService;

    @Autowired
    private NftNumberService nftNumberService;

    @PreAuthorize("@ss.hasPermi('web:consumer:recycle:list')")
    @ApiOperation("分页列表")
    @GetMapping("/page/list")
    public R<List<ConsumerRecycleVo>> pageList(ConsumerRecycleQueryDto param) {
        PageList<ConsumerRecycleVo> vo = consumerRecycleService.pageList(param);
        return R.ok(vo.getList(), vo.getTotal());
    }


    @PreAuthorize("@ss.hasPermi('web:consumer:recycle:add')")
    @Log(title = "回收空投新增", businessType = BusinessType.INSERT)
    @ApiOperation("新增")
    @PostMapping("/add")
    public R add(@RequestBody @Validated ConsumerRecycleSaveDto param) {
        param.setCreateBy(getUserId());
        consumerRecycleService.verifyAndSave(param);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:consumer:recycle:list')")
    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public R<List<ConsumerRecycleDetailVo>> detail(@PathVariable Long id) {
        List<ConsumerRecycleDetailVo> vo = new ArrayList<>();
        ConsumerRecycle consumerRecycle = consumerRecycleService.getById(id);
        for (String nftNumberStr : consumerRecycle.getNftNumberStr().split(",")) {
            ConsumerRecycleDetailVo detailVo = new ConsumerRecycleDetailVo();
            detailVo.setFromUserId(consumerRecycle.getFromUserId());
            detailVo.setToUserId(consumerRecycle.getToUserId());
            detailVo.setType(consumerRecycle.getType());
            detailVo.setNftNumberId(nftNumberService.lambdaQuery().eq(NftNumber::getNumberValueStr, nftNumberStr).one().getId());
            detailVo.setNftNumberStr(nftNumberStr);
            vo.add(detailVo);
        }
        return R.ok(vo);
    }

    @PreAuthorize("@ss.hasPermi('web:consumer:recycle:list')")
    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, IdDto param) {
        List<ConsumerRecycleDetailVo> list = detail(param.getId()).getData();
        ExcelUtil<ConsumerRecycleDetailVo> util = new ExcelUtil<>(ConsumerRecycleDetailVo.class);
        util.exportExcel(response, list, "回收空投数据");
    }
}

